﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using MemcachedProviders.Cache;
using System.Web;
using Pixysoft.Weblications.Sango.Environment;
using Pixysoft.Weblications.Sango.Constant;
using Pixysoft.Weblications.Sango.Formular;

namespace Pixysoft.Weblications.Sango.Controller
{
    public class SectionImageController
    {
        public const int mode_terrain = 1;
        public const int mode_prosperity = 2;
        public const int mode_section = 3;

        private const string memcache_key = "SectionImageController";

        public static byte[] GetTerrianImage(int value)
        {
            string memcachedkey = GetMemcachedKey(mode_terrain, value);
            string filepath = "~/images/sango/section/";
            string filename = string.Format("section_terrain_{0}.gif", value.ToString("00"));

            return GetImage(memcachedkey, filepath, filename);
        }

        // TODO 需要配置繁荣度映射表
        public static byte[] GetProsperityImage(int orgprosperity, int sectionSerialcode)
        {
            string memcachedkey = GetMemcachedKey(mode_prosperity, orgprosperity + sectionSerialcode);
            string filepath = "~/images/sango/section/";

            Entities.Sys_Section section = SysSectionHelper.searchSectionByCode(sectionSerialcode);
            long prosperitystep = SectionFormular.GetSectionMaxProsperityvalue(section.Gradetypecode);
            int prosperity = 1;
            do
            {
                if (orgprosperity < prosperitystep * prosperity)
                {
                    break;
                }

                prosperity++;
            }
            while (true);

            if (prosperity >= 6)
            {
                prosperity = 6;
            }

            string filename = string.Format("section_prosperity_{0}.GIF", prosperity.ToString("00"));

            return GetImage(memcachedkey, filepath, filename);
        }

        public static byte[] GetSectionTypeImage(int value)
        {
            string memcachedkey = GetMemcachedKey(mode_section, value);
            string filepath = "~/images/sango/section/";
            string filename = string.Format("section_type_{0}.GIF", value.ToString("00"));

            return GetImage(memcachedkey, filepath, filename);
        }

        private static byte[] GetImage(string memcachedkey, string filepath, string filename)
        {
            byte[] images = DistCache.Get<byte[]>(memcachedkey);
            if (images != null && images.Length > 0)
            {
                return images;
            }

            string path = HttpContext.Current.Server.MapPath(filepath + filename);
            if (!File.Exists(path))
            {
                return null;
            }

            images = File.ReadAllBytes(path);
            DistCache.Add(memcachedkey, images);
            return images;
        }

        private static string GetMemcachedKey(int mode, int value)
        {
            return memcache_key + mode + value;
        }
    }
}
